//提交校验
function validParam(){
	var securNum = document.getElementById("securNum").value;
	if(!securNum){
		alert("请输入证券代码。");
		return;
	}
	var price = document.getElementById("price").value;
	if(!price){
		alert("请输入买入价格。");
		return;
	}
	var count = document.getElementById("count").value;
	if(!count){
		alert("请输入买入数量。");
		return;
	}
	var appointmentNumber = document.getElementById("appointmentNumber").value;
	if(!appointmentNumber){
		alert("请输入约定号。");
		return;
	}
	var seatOfTheOtherParty = document.getElementById("seatOfTheOtherParty").value;
	if(!seatOfTheOtherParty){
		alert("请输入对方席位。");
		return;
	}
	var mutualShareholder = document.getElementById("mutualShareholder").value;
	if(!mutualShareholder){
		alert("请输入对方股东。");
		return;
	}
	var securNumError = document.getElementById("securNumError").innerHTML;
	if(!!securNumError){
		alert("证券代码不合法，请确认。");
		return;
	}
	var priceError = document.getElementById("priceError").value;
	if(!!priceError){
		alert("买入价格不合法，请确认。");
		return;
	}
	var countError = document.getElementById("countError").value;
	if(!!countError){
		alert("买入数量不合法，请确认。");
		return;
	}
	stockSubmit();
}
// 买入提交
function stockSubmit() {
	$("#submitBtn").attr("disabled", true);
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"stockCode" : document.getElementById("securNum").value,
		"buyingAndSellingPrice" : document.getElementById("price").value,
		"quantity" : document.getElementById("count").value,
		"seclarationType" : "20001",
		"appointmentNumber":document.getElementById("appointmentNumber").value,
		"seatOfTheOtherParty":document.getElementById("seatOfTheOtherParty").value,
		"mutualShareholder":document.getElementById("mutualShareholder").value,
	};
	var para = JSON.stringify(options);
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/buyingstock",
		data : para,
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			$("#submitBtn").attr("disabled", false);
			var data = result.data
			if (data.status) {
				// 查询到的数据
				alert(data.promptMessage);
				queryHistory();
				queryUserInfo("child");
			} else {
				alert(data.promptMessage);
			}
		}
	});
}

var historyOrder = [];
var currentPage = 1;
var totalPage = 1;
// 查询买入交易记录
function queryHistory() {
	var options = {
		"stockStatus" : "50003",
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/queryorders",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			if (result.message == "SUCCESS") {
				// data为请求到的数据
				var data = result.data;
				historyOrder = data;
				totalPage = Math.ceil(data.length / 10);
				if (totalPage === 0) {
					var noData = "<div style='width:100%;height:30px;margin:40px 0;text-align: center;'>暂无表格数据</div>"
					document.getElementById("buyNoData").innerHTML = noData;
				}
				document.getElementById("buyTotal").innerText="";
				$("#buyTotal").append(totalPage);
				dealData();
			} else {
				alert("数据请求错误！");
			}
		}
	});
}

// 处理交易记录表格数据展示
function dealData() {
	var displayOrder = [];
	$("#displayData").find("tr").remove();
	if (totalPage < 1 || currentPage === totalPage) {
		$("#nextPageBtn").attr("disabled", true);
	} else {
		$("#nextPageBtn").attr("disabled", false);
	}
	if (currentPage === 1) {
		$("#prePageBtn").attr("disabled", true);
	} else {
		$("#prePageBtn").attr("disabled", false);
	}
	document.getElementById("currentPage").innerText = currentPage;
	for (var i = (currentPage - 1) * 10; i < currentPage * 10; i++) {
		var item = historyOrder[i];
		if (!item) {
			return;
		}
		displayOrder.push(item);
		row = document.createElement("tr"); // 创建行 
		td1 = document.createElement("td"); // 单号
		td2 = document.createElement("td"); // 数量
		td3 = document.createElement("td"); // 交易时间
		td4 = document.createElement("td"); // 账户
		td5 = document.createElement("td"); // 价格
		td6 = document.createElement("td"); // 状态
		td7 = document.createElement("td"); // 证券号
		td8 = document.createElement("td"); // 证券名称
		td9 = document.createElement("td"); // 关联持仓流水号
		td1.appendChild(document.createTextNode(item.buyRunningWaterNumber)); // 为单元格添加内容 
		td2.appendChild(document.createTextNode(item.quantity));
		td3.appendChild(document.createTextNode(item.seclarationTime));
		td4.appendChild(document.createTextNode(item.accountNumber));
		td5.appendChild(document.createTextNode(item.buyUnitPrice));
		td6.appendChild(document.createTextNode(item.stockStatus));
		td7.appendChild(document.createTextNode(item.stockCode));
		td8.appendChild(document.createTextNode(item.stockCodeName));
		td9.appendChild(document.createTextNode(item.serialNumber));
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(td6);
		row.appendChild(td7);
		row.appendChild(td8);
		row.appendChild(td9);
		$("#displayData").append(row);
		
	}
}
// 上一页
function prePage() {
	currentPage--;
	dealData();
}
// 下一页
function nextPage() {
	currentPage++;
	dealData();
}